﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Contacto
{
    public partial class editar : Form
    {
        public static string w_name;
        public static string w_lastname;
        public static string w_day;
        public static string w_mes;
        public static string w_year;
        public static string w_phone;
        public static string w_mail;
        public static string w_address;

        public editar()
        {
            InitializeComponent();
        }

        private void tx_day_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ' ')
            {
                e.KeyChar = '\0';
            }
            if (Char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void brn_Cancelar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Abrir_Click(object sender, EventArgs e)
        {
            if (tx_name.Text != " " && tx_name.Text != "")
            {
                string nombre = tx_name.Text;
                string r_name = @"Contactos\\" + nombre + "\\name", r_last = @"Contactos\\" + nombre + "\\lastname", r_day = @"Contactos\\" + nombre + "\\day", r_mes = @"Contactos\\" + nombre + "\\mes", r_year = @"Contactos\\" + nombre + "\\year", r_phone = @"Contactos\\" + nombre + "\\phone", r_mail = @"Contactos\\" + nombre + "\\mail", r_address = @"Contactos\\" + nombre + "\\address";
                if (System.IO.File.Exists(r_name))
                {
                    string w_name, w_lastname, w_day, w_mes, w_year, w_phone, w_mail, w_address;

                    System.IO.StreamReader temp0 = new System.IO.StreamReader(r_name);
                    w_name = temp0.ReadToEnd();
                    temp0.Close();
                    System.IO.StreamReader temp1 = new System.IO.StreamReader(r_last);
                    w_lastname = temp1.ReadToEnd();
                    temp1.Close();
                    System.IO.StreamReader temp2 = new System.IO.StreamReader(r_day);
                    w_day = temp2.ReadToEnd();
                    temp2.Close();
                    System.IO.StreamReader temp3 = new System.IO.StreamReader(r_mes);
                    w_mes = temp3.ReadToEnd();
                    temp3.Close();
                    System.IO.StreamReader temp4 = new System.IO.StreamReader(r_year);
                    w_year = temp4.ReadToEnd();
                    temp4.Close();
                    System.IO.StreamReader temp5 = new System.IO.StreamReader(r_phone);
                    w_phone = temp5.ReadToEnd();
                    temp5.Close();
                    System.IO.StreamReader temp6 = new System.IO.StreamReader(r_mail);
                    w_mail = temp6.ReadToEnd();
                    temp6.Close();
                    System.IO.StreamReader temp7 = new System.IO.StreamReader(r_address);
                    w_address = temp7.ReadToEnd();
                    temp7.Close();
                    tx_lastname.Text = w_lastname;
                    tx_day.Text = w_day;
                    tx_mes.Text = w_mes;
                    tx_year.Text = w_year;
                    tx_phone.Text = w_phone;
                    tx_mail.Text = w_mail;
                    tx_address.Text = w_address;
                }
                else
                {
                    MessageBox.Show("No existe ese contacto", "Aviso de error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    tx_name.Text = "";
                }
            }
        }

        private void btn_Guardar_Click(object sender, EventArgs e)
        {
            string name, lastname, day, mes, year, phone, mail, address;
            name = tx_name.Text; lastname = tx_lastname.Text; day = tx_day.Text; mes = tx_mes.Text; year = tx_year.Text; phone = tx_phone.Text; mail = tx_mail.Text; address = tx_address.Text;
            string rt_name = @"Contactos\\" + name + "\\name", rt_lastname = @"Contactos\\" + name + "\\lastname", rt_day = @"Contactos\\" + name + "\\day", rt_mes = @"Contactos\\" + name + "\\mes", rt_year = @"Contactos\\" + name + "\\year", rt_phone = @"Contactos\\" + name + "\\phone", rt_mail = @"Contactos\\" + name + "\\mail", rt_address = @"Contactos\\" + name + "\\address";
            if (editar.w_name != name)
            {
                System.IO.StreamWriter f_name = new System.IO.StreamWriter(rt_name);
                f_name.WriteLine(name);
                f_name.Close();
            }
            if (editar.w_lastname != lastname)
            {
                System.IO.StreamWriter f_last = new System.IO.StreamWriter(rt_lastname);
                f_last.WriteLine(lastname);
                f_last.Close();
            }
            if (editar.w_day != day)
            {
                System.IO.StreamWriter f_day = new System.IO.StreamWriter(rt_day);
                f_day.WriteLine(day);
                f_day.Close();
            }
            if (editar.w_mes != mes)
            {
                System.IO.StreamWriter f_mes = new System.IO.StreamWriter(rt_mes);
                f_mes.WriteLine(mes);
                f_mes.Close();
            }
            if (editar.w_year != year)
            {
                System.IO.StreamWriter f_year = new System.IO.StreamWriter(rt_year);
                f_year.WriteLine(year);
                f_year.Close();
            }
            if (editar.w_phone != phone)
            {
                System.IO.StreamWriter f_phone = new System.IO.StreamWriter(rt_phone);
                f_phone.WriteLine(phone);
                f_phone.Close();
            }
            if (editar.w_mail != mail)
            {
                System.IO.StreamWriter f_mail = new System.IO.StreamWriter(rt_mail);
                f_mail.WriteLine(mail);
                f_mail.Close();
            }
            if (editar.w_address != address)
            {
                System.IO.StreamWriter f_address = new System.IO.StreamWriter(rt_address);
                f_address.WriteLine(address);
                f_address.Close();
            }
            this.Close();
        }
    }
}
